function red() {
  console.log('red');
}
function green() {
  console.log('green');
}
function yellow() {
  console.log('yellow');
}
var light = function (timer, cb, immediate, times) {
  return new Promise((resolve, reject) => {
    if (immediate && times === 0) {
      cb()
      setTimeout(() => {
        resolve()
      }, timer)
    } else {
      setTimeout(() => {
        cb()
        resolve()
      }, timer)
    }
  })
}
let maxCount = 10
var step = function (times) {
  if (times > maxCount) return
  Promise.resolve().then(() => {
    return light(1000, red, true, times)
  }).then(() => {
    return light(1000, green)
  }).then(() => {
    return light(1000, yellow)
  }).then(() => {
    step(times+1)
  })
}
step(0)